package main

import (
	"ginfast/configs"
	"ginfast/db"
	"ginfast/pkg/log"
	"ginfast/router"
	"os"

	svc "github.com/kardianos/service"
)

type program struct {
}

func (p *program) Start(s svc.Service) error {
	if err := configs.Load("config.yaml"); err != nil {
		return err
	}
	return p.run()
}

func (p *program) run() error {
	log.ZapLogger.Info("connect database.", db.Run())
	return router.Run()
}

func (p *program) Stop(s svc.Service) error {
	return router.Shutdown()
}

func main() {
	svconf := &svc.Config{
		Name:        "ginfast",
		DisplayName: "ginfast",
		Description: "This is ginfast application",
	}
	s, err := svc.New(&program{}, svconf)
	if err != nil {
		log.ZapLogger.Fatal(err)
	}
	if len(os.Args) > 1 {
		if os.Args[1] == "install" {
			err = s.Install()
		} else if os.Args[1] == "uninstall" {
			err = s.Uninstall()
		}
		log.ZapLogger.Info(err)
		return
	}
	err = s.Run()
	if err != nil {
		log.ZapLogger.Fatal(err)
	}
}
